#include "msgbusimpl.h"
#include "broker.h"
#include "consumer.h"
#include "producer.h"
#include "agent.h"

CMsgBusImpl::CMsgBusImpl(void)
{
}

CMsgBusImpl::~CMsgBusImpl(void)
{
	logi("CMessageBusImpl::~CMessageBusImpl");
}
std_method_impl CMsgBusImpl::Init(int Argc, basic_tchar* Argv[], IBase* pRot, IBase* pBase)
{
	rc_assert(pRot != NULL, E_FAIL)
	return pRot->QueryInterface(IID_IComRunningObjectTable, (void**)&m_pRot);
}
std_method_impl CMsgBusImpl::Uninit()
{
	HRESULT hr = S_OK;

	return hr;
}
std_method_impl CMsgBusImpl::Start(_pinstance hInstance, UINT uType)
{
	HRESULT hr = S_OK;
	hr = m_pRot->GetObject(CLSID_IAsynFrame, IID_IAsynFrame, (IBase**)&m_pIAsynFrame);
	rc_assert(hr == S_OK, E_FAIL)
	rc_assert(m_pIAsynFrame.m_p != INULL, E_FAIL)
	return hr;
}
std_method_impl CMsgBusImpl::Stop(UINT uExitCode)
{
	HRESULT hr = S_OK;
	return hr;
}
std_method_impl CMsgBusImpl::CreateNode(NODE_CLASS cls, IBase** pBase)
{
	HRESULT hr = E_FAIL;

	if (IS_BROKER(cls))
	{
		CBrokerImpl *p = ALLOC_NEW CBrokerImpl();
		rc_assert(p != NULL, E_FAIL)

		hr = p->CreateSChannel(m_pRot);
		rc_assert(hr == S_OK, E_FAIL)

		hr = p->OpenCChannel();
		rc_assert(hr == S_OK, E_FAIL)

		return p->QueryInterface(IID_IBroker, (void**)pBase);
	}
	else if (IS_PRODUCER(cls)) {

		CProducerImpl *p = ALLOC_NEW CProducerImpl();
		rc_assert(p != NULL, E_FAIL)

		hr = p->CreateCChannel(m_pRot);
		rc_assert(hr == S_OK, E_FAIL)

		hr = p->OpenCChannel();
		rc_assert(hr == S_OK, E_FAIL)

		return p->QueryInterface(IID_IProducer, (void**)pBase);
	}
	else if (IS_CONSUMER(cls)) {

		CConsumerImpl *p = ALLOC_NEW CConsumerImpl();
		rc_assert(p != NULL, E_FAIL)

		hr = p->CreateCChannel(m_pRot);
		rc_assert(hr == S_OK, E_FAIL)

		hr = p->OpenCChannel();
		rc_assert(hr == S_OK, E_FAIL)
	
		return p->QueryInterface(IID_IConsumer, (void**)pBase);
	}
	else if (IS_AGENT(cls)) {

		CAgentImpl *p = ALLOC_NEW CAgentImpl();
		rc_assert(p != NULL, E_FAIL)

		hr = p->CreateCChannel(m_pRot);
		rc_assert(hr == S_OK, E_FAIL)

		hr = p->OpenCChannel();
		rc_assert(hr == S_OK, E_FAIL)

		return p->QueryInterface(IID_IAgent, (void**)pBase);
	}
	else if (IS_REQ(cls)) {

	}
	else if (IS_REP(cls)) {

	}

	return hr;
}

